package com.example.easeimpro.controller.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;

import com.example.easeimpro.R;
import com.example.easeimpro.model.bean.Model;
import com.hyphenate.chat.EMClient;
import com.hyphenate.chat.EMGroupManager;
import com.hyphenate.chat.EMGroupOptions;
import com.hyphenate.easeui.widget.EaseTitleBar;
import com.hyphenate.exceptions.HyphenateException;

import java.util.List;

/**
 * 创建新群
 */
public class CreateGroupActivity extends BaseActivity {
    private EaseTitleBar mFriendTitlebar;
    private EditText mEtNewgroupName;
    private EditText mEtNewgroupDesc;
    private CheckBox mCbNewgroupPublic;
    private CheckBox mCbNewgroupInvite;
    private Button mBtNewgroupCreate;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_create_group);

        initView();
        initData();
        initListener();
    }

    private void initListener() {
        //创建按钮事件处理
        mBtNewgroupCreate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext,PickContactActivity.class);
                startActivityForResult(intent,1);
            }
        });
    }

    private static final String TAG = "CreateGroupActivity";

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
     //   Log.e(TAG, "onActivityResult: " +resultCode);
        if (resultCode == RESULT_OK){

            createGroup(data.getStringArrayExtra("members"));
        }
    }

    private void createGroup(String[] list) {//创建新群聊
        Model.getInstance().getGlobalThreadPool().execute(new Runnable() {
            @Override
            public void run() {
                String groupName = mEtNewgroupName.getText().toString();
                String groupDesc = mEtNewgroupDesc.getText().toString();

                        //参数1群名称 参数2 群简介 参数3 群成员 参数4原因 参数5 参数设置
                EMGroupOptions option = new EMGroupOptions();
                option.maxUsers = 200;
                EMGroupManager.EMGroupStyle groupStyle = null;
                if(mCbNewgroupPublic.isChecked()){//公开
                    if(mCbNewgroupInvite.isChecked()){
                        //开放了群邀请
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePublicOpenJoin;
                    }else{
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePublicJoinNeedApproval;
                    }
                }else{
                    if(mCbNewgroupInvite.isChecked()){
                        //开放了群邀请
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePrivateMemberCanInvite;
                    }else{
                        groupStyle = EMGroupManager.EMGroupStyle.EMGroupStylePrivateOnlyOwnerInvite;
                    }
                }
                option.style = groupStyle;//创建群类型设置
                try {
                    EMClient.getInstance().groupManager().createGroup(groupName,groupDesc,list,"申请加入",option);
                    showToast("群创建成功");
                   finish();
                } catch (HyphenateException e) {
                    showToast("群创建失败");
                    e.printStackTrace();
                }
            }
        });
    }

    private void initData() {
        mFriendTitlebar.setTitle("创建群聊");
        mFriendTitlebar.setOnBackPressListener(new EaseTitleBar.OnBackPressListener() {
            @Override
            public void onBackPress(View view) {
                finish();
            }
        });

    }

    private void initView() {
        mFriendTitlebar = (EaseTitleBar) findViewById(R.id.friend_titlebar);
        mEtNewgroupName = (EditText) findViewById(R.id.et_newgroup_name);
        mEtNewgroupDesc = (EditText) findViewById(R.id.et_newgroup_desc);
        mCbNewgroupPublic = (CheckBox) findViewById(R.id.cb_newgroup_public);
        mCbNewgroupInvite = (CheckBox) findViewById(R.id.cb_newgroup_invite);
        mBtNewgroupCreate = (Button) findViewById(R.id.bt_newgroup_create);
    }
}